//
//  ERGuideVC.m
//  EnjoyRunning
//
//  Created by ljm on 2017/3/16.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ERGuideVC.h"
#import "ERTHTVC.h"

/*
 *    第一种推送界面方式的需要
 */
#import "AppDelegate.h"
#import "ERTHTVC.h"

@interface ERGuideVC ()<UIScrollViewDelegate>

/*
 *    左右滑动的scrollview
 */
@property(nonatomic,strong) UIScrollView *myScrollview;

/*
 *    页面
 */
@property(nonatomic,strong) UIPageControl *myPagecontrol;
@end

@implementation ERGuideVC



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
}

#pragma mark -- 页面设置
- (void) setupUI
{
    [self setupScrollview];
    [self setupPagecontrol];
}

#pragma mark -- 设置scrollview 滚动视图
- (void) setupScrollview
{
    /*
     *    初始化scrollview
     */
    _myScrollview = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    /*
     *    设置内容视图的大小
     */
    _myScrollview.contentSize = CGSizeMake(kwidth * 4, kheight);
    
    /*
     *    点击翻页、翻页效果、是否完全显示当前页
     */
    _myScrollview.pagingEnabled = YES ;
    
    /*
     *    去除弹簧效果
     */
    _myScrollview.bounces = NO ;
    
    /*
     *    去除滚动条
     */
    _myScrollview.showsHorizontalScrollIndicator = NO ;
    
    /*
     *    遵守代理
     */
    _myScrollview.delegate = self;
    
    /*
     *    把图片贴到视图上，循环添加
     */
    for (NSInteger index = 0; index < 4; index ++) {
        
        /*
         *  初始化图片，设置位置偏移量
         */
        UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(index * kwidth, 0, kwidth, kheight)];

        /*
         *    添加图片
         */
        myImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"s-%ld.jpg",index+1]];
        /*
         *    照片添加到scrollview
         */
        [_myScrollview addSubview:myImageView];
    }
    
    /*
     *    添加到当前主视图
     */
    [self.view addSubview:_myScrollview];
    
    
    
    /*
     *    添加按钮、适配；减去50是因为按钮计算按钮本身的长度
     */
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(3.5*kwidth - 50, kheight * 0.85f, 100, 40)];
    
    /*
     *    按钮的背景颜色
     */
    button.backgroundColor = [UIColor clearColor];
    
    /*
     *    给按钮切圆角
     */
    button.layer.cornerRadius = 5.f ;
    button.layer.masksToBounds = YES;
    
    /*
     *    按钮的文字
     */
//    [button setTitle:@"马上体验" forState:UIControlStateNormal];
    
    /*
     *    按钮发生的action  事件
     */
    [button addTarget:self action:@selector(showTHTVC:) forControlEvents:UIControlEventTouchUpInside];
    
    /*
     *    添加到滑动视图
     */
    [_myScrollview addSubview:button];
}

#pragma mark -- 设置page 页码指示器
- (void) setupPagecontrol
{
    
    /*
     *    初始化、屏幕适配
     */
    _myPagecontrol = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    
    /*
     *    确定页码指示器的中心位置
     */
    _myPagecontrol.center = CGPointMake(kwidth/2, kheight * 0.93f);
    
    /*
     *    指示器的个数
     */
    _myPagecontrol.numberOfPages = 4 ;
    
    /*
     *    当前指示器的颜色
     */
    _myPagecontrol.currentPageIndicatorTintColor = [UIColor grayColor];
    
    /*
     *    其他指示器的颜色
     */
    _myPagecontrol.pageIndicatorTintColor = [UIColor whiteColor];
    
    /*
     *    添加
     */
    [self.view addSubview:_myPagecontrol];
    
}

#pragma mark -- 拖动翻页
/*
 *    设置scrollview滑动结束的偏移量和当前页
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    /*
     *    scrollview的横向偏移量除以屏幕宽度就是当前页
     */
    NSInteger index = _myScrollview.contentOffset.x/kwidth;
    
    /*
     *    根据scrollview的偏移量算出当前页
     */
    _myPagecontrol.currentPage = index;
}

#pragma mark -- action
- (void) showTHTVC:(UIButton *)btn
{
    //方法一
    //得到tabbar
//    //得到app自己
//        UIApplication* app = [UIApplication sharedApplication];
//        //通过app得到app的Dlelegate对象
//    
//        AppDelegate* delegate = app.delegate;
//    
//        //推出tabbar
//        [self presentViewController:delegate.THTVC animated:YES completion:nil];
    
    //方法二
    ERTHTVC *tabbar = [[ERTHTVC alloc]init];
    
    [self presentViewController:tabbar animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
